package com.wolfheader.wolfgame.service.impl;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.wolfheader.wolfgame.dao.MemberDao;
import com.wolfheader.wolfgame.entity.MemberEntity;
import com.wolfheader.wolfgame.service.MemberService;

@Service("memberServiceImpl")
public class MemberServiceImpl implements MemberService {

	@Resource
	private MemberDao memberDao;

	@Override
	public MemberEntity mergeMemberByWeichat(Map<String, Object> userInfoMap) {
		MemberEntity player = memberDao.findByOpenid(userInfoMap.get("openid").toString());
		if (player == null) {
			player = new MemberEntity();
			player.setOpenid(userInfoMap.get("openid").toString());
			player.setCity(userInfoMap.get("city").toString());
			player.setCountry(userInfoMap.get("country").toString());
			player.setHeadimgurl(userInfoMap.get("headimgurl").toString());
			player.setNickname(userInfoMap.get("nickname").toString());
			player.setProvince(userInfoMap.get("province").toString());
			player.setSex(userInfoMap.get("sex").toString());
			player.setPlayCount(1);
			player.setCreateDate(new Date());
			player.setUpdateDate(new Date());
			player.setToken(userInfoMap.get("token").toString());
			memberDao.save(player);
		} else {
			Integer playerCount = player.getPlayCount() + 1;
			player.setPlayCount(playerCount);
			player.setCity(userInfoMap.get("city").toString());
			player.setCountry(userInfoMap.get("country").toString());
			player.setHeadimgurl(userInfoMap.get("headimgurl").toString());
			player.setNickname(userInfoMap.get("nickname").toString());
			player.setProvince(userInfoMap.get("province").toString());
			player.setSex(userInfoMap.get("sex").toString().toString());
			player.setCreateDate(new Date());
			player.setToken(userInfoMap.get("token").toString());
			memberDao.save(player);
		}
		return player;
	}
}
